# -*- coding: utf-8 -*-
from urllib import urlopen
import sys
import os
import logging
import datetime
#import socket
#socket.setdefaulttimeout(30.0)
from django.utils import simplejson
from django.conf import settings
from xiaonei import Xiaonei
from wuyao import Wuyao
from main.settings import category_image_group, category_image_gae

def url_read(url, time = 5):
    try:
        return urlopen(url).read()
    except:
        if time > 0:
            return url_read(url, time - 1)
        else:
            raise

def get_cover(cate,id):
    if settings.DEBUG:
        return '/upload/%s/%s.jpg' % (category_image_group[cate], id)
    else:
        return category_image_gae[category_image_group[cate]] % id
    
def get_json(request):
    request_obj = {}
    for k,v in request.REQUEST.items(): 
        request_obj[k] = v
    return simplejson.dumps(request_obj)


def get_client(request):
    if 'xn_sig_user' in request.REQUEST:
        return Xiaonei(request)
    elif '51_sig_user' in request.REQUEST:
        return Wuyao(request)
    else:
        return None